import { ref } from 'vue'

const csvHeaders = ref([])
const curveShow = ref([])
const visibility = ref([])

async function getCsvHeadersName(csvPath) {
    try {
        const response = await fetch(csvPath)
        if (!response.ok) {
            throw new Error(`Failed to fetch CSV file: ${response.statusText}`)
        }
        const text = await response.text()
        const lines = text.split('\n')
        const headers = lines[0].split(',')
        csvHeaders.value = headers.map((item, index) => ({
            value: item,
            label: item,
            index,
        }))
        csvHeaders.value.shift()

        curveShow.value = csvHeaders.value.map((item) => ({
            name: item.value,
            index: item.index,
            offsetMultiplication: 1,
            offsetIncrement: 0,
            visible: true,
        }))
        visibility.value = new Array(csvHeaders.value.length).fill(true)
    } catch (error) {
        console.error('Error fetching or parsing CSV headers:', error)
        csvHeaders.value = []
        curveShow.value = []
        visibility.value = []
    }
}

export { csvHeaders, curveShow, visibility, getCsvHeadersName }
